{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "sys.path.append('../utils')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from aoc import * # get_input, d4, d8, d4n\n",
    "from atcoder.math import * # crt, pow_mod, inv_mod, floor_sum\n",
    "from atcoder.convolution import * # convolution\n",
    "from atcoder.string import * # suffix_array, lcp_array, z_algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample = '''ULL\n",
    "RRDDD\n",
    "LURDL\n",
    "UUUUD'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "inp = get_input(2016, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def part1(inp):\n",
    "    board = [\n",
    "        '00000',\n",
    "        '01230',\n",
    "        '04560',\n",
    "        '07890',\n",
    "        '00000',\n",
    "    ]\n",
    "    \n",
    "    ans = ''\n",
    "    x = 2\n",
    "    y = 2\n",
    "    for line in inp.split('\\n'):\n",
    "        if len(line) == 0:\n",
    "            continue\n",
    "        for c in line:\n",
    "            dx, dy = d4n2[c]\n",
    "            nx = x + dx\n",
    "            ny = y + dy\n",
    "            if board[ny][nx] != '0':\n",
    "                x, y = nx, ny\n",
    "        ans += str(board[y][x])\n",
    "    return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1985'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part1(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'92435'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part1_ans = part1(inp)\n",
    "part1_ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "submit_answer(part1_ans, 2016, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def part2(inp):\n",
    "    board = [\n",
    "        '0000000',\n",
    "        '0001000',\n",
    "        '0023400',\n",
    "        '0567890',\n",
    "        '00ABC00',\n",
    "        '000D000',\n",
    "        '0000000',\n",
    "    ]\n",
    "    \n",
    "    ans = ''\n",
    "    x = 1\n",
    "    y = 3\n",
    "    for line in inp.split('\\n'):\n",
    "        if len(line) == 0:\n",
    "            continue\n",
    "        for c in line:\n",
    "            dx, dy = d4n2[c]\n",
    "            nx = x + dx\n",
    "            ny = y + dy\n",
    "            if board[ny][nx] != '0':\n",
    "                x, y = nx, ny\n",
    "        ans += board[y][x]\n",
    "    return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'5DB3'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part2(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C1A88'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "part2_ans = part2(inp)\n",
    "part2_ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "submit_answer(part2_ans, 2016, 2, level=2)"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
